﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeBuilder.Utility
{
    public static class EDMMapping
    {
        public static MappingResult MappTo(this string source, bool isNullable, int precision)
        {
            var result = new MappingResult();
            switch (source)
            {
                #region 数字类型
                case "int":
                    result.ConvertType = "int".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Number;
                    break;
                case "smallint":
                    result.ConvertType = "short".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Digits;
                    break;
                case "float":
                    result.ConvertType = "double".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Number;
                    break;
                case "bigint":
                    result.ConvertType = "long".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Number;
                    break;
                case "real":
                    result.ConvertType = "float".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Number;
                    break;
                #endregion

                #region 金额
                case "decimal":
                    result.ConvertType = "decimal".NullString(isNullable);
                    result.InitValue = "0";
                    result.InputType = InputType.Number;
                    break;
                case "numeric":
                    result.ConvertType = "decimal".NullString(isNullable);
                    result.InitValue = "0";
                    result.ColumnTypeName = "[Column(TypeName = \"numeric\")]";
                    result.InputType = InputType.Number;
                    break;
                case "smallmoney":
                    result.ConvertType = "decimal".NullString(isNullable);
                    result.InitValue = "0";
                    result.ColumnTypeName = "[Column(TypeName = \"smallmoney\")]";
                    result.InputType = InputType.Number;
                    break;
                case "money":
                    result.ConvertType = "decimal".NullString(isNullable);
                    result.InitValue = "0";
                    result.ColumnTypeName = "[Column(TypeName = \"money\")]";
                    result.InputType = InputType.Number;
                    break;
                #endregion

                #region 日期
                case "datetime":
                    result.ConvertType = "DateTime".NullString(isNullable);
                    result.InitValue = "System.DateTime.Now";
                    result.InputType = InputType.DateTime;
                    break;
                case "datetime2":
                    result.ConvertType = "DateTime".NullString(isNullable);
                    result.ColumnTypeName = "[Column(TypeName = \"datetime2\")]";
                    result.InitValue = "System.DateTime.Now";
                    result.InputType = InputType.DateTime;
                    break;
                case "date":
                    result.ConvertType = "DateTime".NullString(isNullable);
                    result.ColumnTypeName = "[Column(TypeName = \"date\")]";
                    result.InitValue = "System.DateTime.Now";
                    result.InputType = InputType.DateTime;
                    break;
                case "time":
                    result.ConvertType = "TimeSpan".NullString(isNullable);
                    break;
                case "datetimeoffset":
                    result.ConvertType = "DateTimeOffset";
                    break;
                case "smalldatetime":
                    result.ConvertType = "DateTime";
                    result.ColumnTypeName = "[Column(TypeName = \"smalldatetime\")]";
                    result.InitValue = "System.DateTime.Now";
                    break;
                #endregion

                #region 字符串
                case "nvarchar":
                    result.ConvertType = "string";
                    result.LengthString = "[StringLength("+precision+")]";
                    break;
                case "nchar":
                    result.ConvertType = "string";
                    result.LengthString = "[StringLength("+precision+")]";
                    break;
                case "varchar":
                    result.ConvertType = "string";
                    result.LengthString = "[StringLength("+precision+")]";
                    break;
                case "char":
                    result.ConvertType = "string";
                    result.LengthString = "[StringLength("+precision+")]";
                    break;
                case "varchar(max)":
                    result.ConvertType = "string";
                    break;
                case "nvarchar(max)":
                    result.ConvertType = "string";
                    break;
                case "ntext":
                    result.ConvertType = "string";
                    result.ColumnTypeName = "[Column(TypeName = \"ntext\")]";
                    break;
                case "text":
                    result.ConvertType = "string";
                    result.ColumnTypeName = "[Column(TypeName = \"text\")]";
                    break;
                case "xml":
                    result.ConvertType = "string";
                    result.ColumnTypeName = "[Column(TypeName = \"xml\")]";
                    break;
                #endregion

                #region 其他
                case "bit":
                    result.ConvertType = "bool".NullString(isNullable);
                    result.InitValue = "false";
                    result.InputType = InputType.CheckBox;
                    break;
                case "uniqueidentifier":
                    result.ConvertType = "Guid".NullString(isNullable);
                    result.InitValue = "Guid.NewGuid()";
                    break;
                case "geography":
                    result.ConvertType = "DbGeography";
                    break;
                case "geometry":
                    result.ConvertType = "DbGeography";
                    break;
                #endregion

                #region 二进制
                case "varbinary":
                    result.ConvertType = "byte[]";
                    result.LengthString = "[MaxLength("+precision+")]";
                    break;
                case "tinyint":
                    result.ConvertType = "byte".NullString(isNullable);
                    break;

                case "binary":
                    result.ConvertType = "byte[]";
                    result.LengthString = "[MaxLength("+precision+")]";
                    break;
                case "varbinary(max)":
                    result.ConvertType = "byte[]";
                    break;
                case "image":
                    result.ConvertType = "byte[]";
                    break;
                case "timestamp":
                    result.ConvertType = "byte[]";
                    result.LengthString = "[MaxLength("+precision+")]";
                    result.ColumnTypeName = "[Column(TypeName = \"timestamp\")]\r\n\t[Timestamp]";
                    break;
                #endregion
            }

            return result;
        }

        static string NullString(this string str, bool isNullable)
        {
            return str + (isNullable ? "?" : "");
        }

        public class MappingResult
        {
            public MappingResult()
            {
                this.InitValue =null;
            }
            /// <summary>
            /// 转换成C#的类型
            /// </summary>
            public string ConvertType { get; set; }
            /// <summary>
            /// 默认值
            /// </summary>
            public string InitValue { get; set; }
            /// <summary>
            /// [Column(TypeName = "")]
            /// </summary>
            public string ColumnTypeName { get; set; }
            /// <summary>
            /// [MaxLength(50)],[StringLength(50)]
            /// </summary>
            public string LengthString { get; set; }
            /// <summary>
            /// 输入框类型
            /// </summary>

            public InputType InputType { get; set; }
        }

        public enum InputType
        {
            Text,
            CheckBox,
            TextArea,
            Number,
            Digits,
            DateTime
        }
    }
}
